/******************************************************************************
 *      Mestrado em Ciência da Computação - UFMS
 *		Redes de Computadores - Prof. Dr. Ronaldo Alves Ferreira
 *		Alunos: Geraldo Landre, Marcel Tolentino, Vinícius Bueno
 *****************************************************************************/
 
#ifndef REQUEST_H
#define REQUEST_H

#include "vstring.h"


#include "base.h"
#include "config.h"
#include "cache.h"

#define BUFFER		1024

#define CR "\r"
#define CRLF "\r\n" // só \n para que o strtok funcione
#define SPACE " "

#define HTTP_VERSION "HTTP/1.1"

#define STR_GET		"GET"
#define STR_HEAD	"HEAD"
#define STR_POST	"POST"
#define STR_PUT		"PUT"
#define STR_DELETE	"DELETE"
#define STR_TRACE	"TRACE"
#define STR_CONNECT	"CONNECT"

#define URI_MAX_LEN		256		// tamanho máximo de dir/file UNIX

/*	Erros HTTP	*/
typedef enum
{
    MSG_CONTINUE =                      100,
    MSG_SWITCHING_PROTOCOLS =           101,
    MSG_OK =				200, // OK
    MSG_CREATED =                       201,
    MSG_ACCEPTED =                      202,
    MSG_NON_AUTHORITATIVE_INFORMATION = 203,
    MSG_NO_CONTENT =                    204,
    MSG_RESET_CONTENT =                 205,
    MSG_PARTIAL_CONTENT =               206,
    ERR_MULTIPLE_CHOICES =              300,
    ERR_MOVED_PERMANENTLY =             301,
    ERR_FOUND =                         302,
    ERR_SEE_OTHER =                     303,
    ERR_NOT_MODIFIED =                  304,
    ERR_USE_PROXY =                     305,
    ERR_TEMPORARY_REDIRECT =            307,
    ERR_BAD_REQUEST =			400,
    ERR_UNAUTHORIZED =                  401,
    ERR_PAYMENT_REQUIRED =              402,
    ERR_FORBIDDEN =             	403,
    ERR_NOT_FOUND =			404,
    ERR_METHOD_NOT_ALLOWED =            405,
    ERR_NOT_ACCEPTABLE =                406,
    ERR_PROXY_AUTHENTICATION_REQUIRED = 407,
    ERR_REQUEST_TIME_OUT =              408,
    ERR_CONFLICT =                      409,
    ERR_GONE =                          410,
    ERR_LENGTH_REQUIRED =               411,
    ERR_REQUEST_URI_TOO_LONG =          414,
    ERR_INTERNAL_SERVER_ERROR =		500,
    ERR_NOT_IMPLEMENTED =		501,
    ERR_SERVICE_UNAVAILABLE =		503,
    ERR_HTTP_VERSION_NOT_SUPPORTED =	505,

/*
| "412" ; Section 10.4.13: Precondition Failed
| "413" ; Section 10.4.14: Request Entity Too Large
| "415" ; Section 10.4.16: Unsupported Media Type
| "416" ; Section 10.4.17: Requested range not satisfiable
| "417" ; Section 10.4.18: Expectation Failed
| "502" ; Section 10.5.3: Bad Gateway
| "504" ; Section 10.5.5: Gateway Time-out*/

} HTTP_ERROR;

typedef struct
{
    int error;
    int method;
    int alive;    
    int content_length;
    int stack;
    int cgi_bin; // indicador se a requisição é para cgi-bin ou não
    int end;  // indicador de que o header acabou (contar quantos enters chegou)
    vstring *request;
    vstring *http;
    vstring *port;
    vstring *path;
    vstring *file;
    vstring *msg;
    vstring *host;
    vstring *method_str;
    vstring **header;
}header_data;

header_data* new_header();

void reset_header(header_data **hd);

void copy_header_data(header_data **oldhd,header_data **newhd);

void print_header_data(const header_data *hd);

char* get_str_httperror(int error);

int is_cacheable(header_data *hd);

int is_finished(header_data *hd);

#endif
